package com.qiwo.bpm.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Service;

import com.qiwo.bpm.dao.DefinitionDao;
import com.qiwo.bpm.domain.FlowDefinition;
import com.qiwo.bpm.service.FlowDefinitionService;
import com.qiwo.common.base.CoreServiceImpl;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;

/**
 * 
 * <pre>
 * 流程定义表
 * </pre>
 *  @author  chenwenchao
 *  @date 2021-04-01 16:15:01
 * <small> 2021-04-01 16:15:01 | chenwenchao</small>
 */
@Service
public class FlowDefinitionServiceImpl extends CoreServiceImpl<DefinitionDao, FlowDefinition> implements FlowDefinitionService {

    @Override
    public void doImport(String tmpFile, String typeId) {

    }

    @Override
    public void save(String id, String flowxml, Integer flowTypeID) throws Exception {
        FlowDefinition flowDefinition;
        if (StringUtils.isNotBlank(id)) {
             flowDefinition = new FlowDefinition();
            flowDefinition.setState(0);
            flowDefinition.setFlowType(flowTypeID);
        } else {
            flowDefinition = (FlowDefinition)this.baseMapper.selectById(id);
        }
        InputStream inputStream = new ByteArrayInputStream(flowxml.getBytes("UTF-8"));
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(inputStream);
        Element rootProcess = doc.getRootElement();
        flowDefinition.setRemark(rootProcess.attributeValue("remark"));
        flowDefinition.setFlowName(rootProcess.attributeValue("name"));
        flowDefinition.setSort(Integer.parseInt(rootProcess.attributeValue("sort")));
        flowDefinition.setFlowXml(flowxml);
        this.baseMapper.insert(flowDefinition);
    }

    @Override
    public String flowXmlEnter(String flowxml)
            throws Exception
    {
        if (StringUtils.isEmpty(flowxml)) {
            return "";
        }
        Pattern p = Pattern.compile("\r|\n");
        Matcher m = p.matcher(flowxml);
        String after = m.replaceAll("");
        return after;
    }


    public void flowIssue(String[] ids, Integer state)
            throws Exception
    {
        for (String id : ids) {
            FlowDefinition flowDefinition = (FlowDefinition)this.baseMapper.selectById(id);
            flowDefinition.setState(state);
            this.baseMapper.insert(flowDefinition);
        }
    }

    public void copyDefinition(String id, int copies, Integer type)
            throws Exception
    {
        if (copies < 1)
            return;
        int i = copies;
        FlowDefinition definition = (FlowDefinition)this.baseMapper.selectById(id);
        do {
            FlowDefinition copy = new FlowDefinition();
            BeanUtils.copyProperties(copy, definition);
            copy.setId(null);
            copy.setFlowName("【复制】" + copy.getFlowName());
            copy.setState(0);
            if (type!=null)
                copy.setFlowType(type);
            this.baseMapper.insert(copy);

            i--; } while (i >= 0);
    }
}
